package controllers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import redstone.xmlrpc.XmlRpcFault;
import server.PostBlogger;
import server.PostWp;

import com.google.gdata.util.ServiceException;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import data.Data;

@SuppressWarnings("serial")
public class GetContentServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		Utils.checkLoginFalse(request, response);
		response.setContentType("html/plain");
		HttpSession session = request.getSession();
		DBObject object = (DBObject) session.getAttribute("users");
		if (object != null) {
			DBCollection collection = Data.data("urlCraw");
			DBCursor cursor = collection.find(new BasicDBObject("username",
					object.get("username").toString()));

			request.setAttribute("cursor", cursor);
		}
		String[] listError = new String[5];
		request.setAttribute("listError", listError);
		try {
			request.getRequestDispatcher("/get-content.jsp").forward(request,
					response);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		Utils.checkLoginFalse(request, response);
		HttpSession session = request.getSession();
		DBObject object = (DBObject) session.getAttribute("users");
		
		response.setCharacterEncoding("utf-8");
		String sitemap = request.getParameter("sitemap");
		String regtitle = request.getParameter("regtitle");
		String regcontent = request.getParameter("regcontent");
		String regremove = request.getParameter("regremove");
		String typeLink = request.getParameter("typeLink");
		String source = request.getParameter("source");
		
		if(sitemap!=null&&regtitle!=null&&regcontent!=null)
		{
			sitemap = Utils.fomatSitemap(sitemap);
			
			URL url = new URL(sitemap);
			String charset = "UTF-8";
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		    connection.setInstanceFollowRedirects(true);
		    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 AlexaToolbar/alxf-2.14 AlexaToolbar/pXVUnvuf-1.2 Firefox/3.6.21");
			connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
			connection.setRequestProperty("Accept-Charset", "	ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setRequestProperty("Keep-Alive", "115");
			connection.setRequestProperty("Cookie", "PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES");
			connection.setRequestProperty("Cache-Control", "max-age=0");
		    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName(charset)));
			String content = "";
			char[] buf = new char[102102];
			int numRead=0;
	        while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            content += readData;
	            buf = new char[102102];
	        }
	        reader.close();
			Document doc = Jsoup.parse(content);
			
			int index = doc.select("meta").toString().toLowerCase()
					.indexOf("charset=");
			if (index != -1) {
				charset = doc.select("meta").toString().substring(index + 8)
						.replaceAll("[^\\'\\\"0-9a-zA-Z\\-\\_]+[\\w\\W]+", "")
						.replaceAll("[\\'\\\"]+", "");
			}
			connection = (HttpURLConnection) url.openConnection();
		    connection.setInstanceFollowRedirects(true);
		    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 AlexaToolbar/alxf-2.14 AlexaToolbar/pXVUnvuf-1.2 Firefox/3.6.21");
			connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
			connection.setRequestProperty("Accept-Charset", "	ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setRequestProperty("Keep-Alive", "115");
			connection.setRequestProperty("Cookie", "PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES");
			connection.setRequestProperty("Cache-Control", "max-age=0");
		    reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName(charset)));
	
			content = "";
			buf = new char[102102];
			numRead=0;
	        while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            content += readData;
	            buf = new char[102102];
	        }
	        reader.close();
			doc = Jsoup.parse(content);
			
			String ti = doc.select(regtitle).text();
			doc = Jsoup.parse(doc.select(regcontent).html());
			String[] remove = regremove.split(" ");
			doc.select("script").remove();
			if (remove.length > 0) {
				for (int i = 0; i < remove.length; i++) {
					try {
						if ((remove[i] != null) && (!remove[i].equals("")))
						{
							String[] reg = remove[i].split("\\|"); 
							
							if(reg.length >=2)
							{
								if(reg[1].equals("last"))
								{
									doc.select(reg[0].replaceAll(">", " ")).last().remove();
								} else {
									doc.select(reg[0].replaceAll(">", " ")).first().remove();
								}
							} else {
								doc.select(remove[i].replaceAll(">", " ")).remove();
							}
						}
					} catch (Exception localException1) {
					}
				}
			}
			
			if(typeLink.equals("nofollow"))
			{
				doc.select("a").attr("rel","nofollow");
			}
			
			String strContent = doc.html();
			
			if(source.equals("yes"))
			{
				strContent += "<br/>Source : <a rel='nofollow' href='"+sitemap+"'>"+sitemap+"</a>";
			}
			
			ArrayList<String> term = new ArrayList<String>();
			
			try {
	        	String message = strContent.replaceAll("\\<.*?\\>", "");
	
				url = new URL("http://o2tv.vn/tag.php");
	            connection = (HttpURLConnection) url.openConnection();
	            connection.setReadTimeout(10000);
				connection.setConnectTimeout(10000);
	            connection.setDoOutput(true);
	            connection.setRequestMethod("POST");
	            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
	            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
	
	            writer.write("str="+message.replaceAll("[\\W]+", " "));
	            writer.close();
	            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
		           	 reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		           	 String content2 = "";
		           	 buf = new char[102102];
					 numRead=0;
			         while((numRead=reader.read(buf)) != -1){
			            String readData = String.valueOf(buf, 0, numRead);
			            content += readData;
			            buf = new char[102102];
			         }
			         reader.close();
		           	 doc = Jsoup.parse(content2);
	            }
	        	
	            Elements nou = doc.select(".tag");
	            
	            if(nou.size() > 0)
	            {
	            	for(int i = 0;i<nou.size();i++)
	            	{
			            if(!term.contains(nou.get(i).text().toLowerCase())&& nou.get(i).text().length()>=5 && term.size()<10) {
			            	term.add(nou.get(i).text().toLowerCase());
			            }
			            if(i==10)
			            	break;
	            	}
	            }
	            
	        } catch (Exception e) {
				// TODO: handle exception
			}
	        response.getWriter().println(strContent);
	        try {
	        	if(object.get("type").toString().equals("wordpress"))
	        	{
	        		PostWp.postWp(ti, Utils.cleanContent(strContent), term,object.get("domain").toString());
	        	}
	        	if(object.get("type").toString().equals("blogger"))
	        	{
	        		PostBlogger.postBlogger(ti, Utils.cleanContent(strContent), term,object);
	        	}
				request.setAttribute("success", "Post success");
			} catch (XmlRpcFault e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if (object != null) {
				DBCollection collection = Data.data("urlCraw");
				DBCursor cursor = collection.find(new BasicDBObject("username",
						object.get("username").toString()));
	
				request.setAttribute("cursor", cursor);
			}
			String[] listError = new String[5];
			request.setAttribute("listError", listError);
//			try {
//				request.getRequestDispatcher("/get-content.jsp").forward(request,
//						response);
//			} catch (Exception e) {
//				// TODO: handle exception
//			}
		}
	}
}
